<template>
  <div class="left_record">
    <div class="header">
      <el-button type="primary" class="add" @click="openDialog">
        新增
      </el-button>
    </div>
    <el-table :data="list" style="width: 100%">
      <el-table-column label="标题" width="200">
        <template #default="{ row }">
          <div class="title text">{{ row.title }}</div>
        </template>
      </el-table-column>
      <el-table-column label="内容">
        <template #default="{ row }">
          <div class="title text">{{ row.content }}</div>
        </template>
      </el-table-column>
      <el-table-column label="图片" width="200">
        <template #default="{ row }">
          <img :src="row.image" class="poster" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template #default="{ row }">
          <el-button type="warning" @click="updateData(row.id)">编辑</el-button>
          <el-popconfirm
            title="确定要删除此轮播图吗"
            @confirm="deleteData(row.id)"
          >
            <template #reference>
              <el-button type="danger">删除</el-button>
            </template>
            <template #actions="{ confirm, cancel }">
              <el-button size="small" @click="cancel">取消</el-button>
              <el-button type="danger" size="small" @click="confirm">
                确定
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>

  <!-- 操作弹层 -->
  <el-dialog :title="optionDoalogTitle" v-model="optionDialogVisible">
    <el-form :model="optionForm" label-width="80px">
      <el-form-item label="标题">
        <el-input
          v-model="optionForm.title"
          placeholder="请输入标题..."
        ></el-input>
      </el-form-item>
      <el-form-item v-for="(item, index) in optionForm.content" label="内容">
        <el-input
          type="textarea"
          :autosize="{ minRows: 5, maxRows: 10 }"
          v-model="optionForm.content[index]"
          placeholder="请输入标题..."
        ></el-input>
      </el-form-item>
      <el-form-item label="图片">
        <ImageView
          :src="[optionForm.image]"
          @addImage="addImage"
          @deleteImage="deleteImage"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="optionDialogVisible = false">取消</el-button>
        <el-button
          v-if="optionDoalogTitle === '新增'"
          type="warning"
          @click="addContent"
          >增加内容</el-button
        >
        <el-button type="primary" @click="submit" :loading="loading"
          >确定</el-button
        >
      </div>
    </template>
  </el-dialog>

  <!-- 分页器 -->
  <el-pagination
    background
    layout="prev, pager, next"
    :page-size="5"
    :total="count"
    @change="pageChangeHandler"
  />
</template>

<script setup lang="ts">
import { watch, reactive } from 'vue'

import ImageView from '@/base-ui/image-view/index.vue'

import { normalOptions } from '@/hooks/normalOptions'
import {
  deleteLeftRecordApi,
  getLeftRecordList,
  updateLeftRecordApi,
  upload,
  addLeftRecordApi
} from '@/api'

const props = defineProps({
  activeName: {
    type: String,
    default: ''
  }
})

const optionForm = reactive({
  // 表单数据
  id: '',
  title: '',
  image: '',
  content: [] as any
})

// 赋值数据
const assignmentData = (row: typeof optionForm) => {
  for (const key in optionForm) {
    optionForm[key as keyof typeof optionForm] = row[key as keyof typeof row]
  }
}

// 清空表单数据
const clearFormData = () => {
  for (const key in optionForm) {
    optionForm[key as keyof typeof optionForm] = ''
  }
  optionForm.content = []
}

const {
  list,
  count,
  optionDoalogTitle,
  optionDialogVisible,
  loading,
  getList,
  pageChangeHandler,
  deleteData,
  updateData,
  uploadFile,
  submitUpdateData,
  openDialog,
  addData
} = normalOptions({
  getList: getLeftRecordList,
  deleteData: deleteLeftRecordApi,
  updateData: updateLeftRecordApi,
  assignmentData: assignmentData,
  uploadFile: upload,
  clearFormData: clearFormData,
  addData: addLeftRecordApi
})

// 选择了描述的图片
const addImage = async (f: FileList) => {
  const res = await uploadFile(f)
  optionForm.image = res
}

// 删除描述的图片
const deleteImage = () => {
  optionForm.image = ''
}

// 点击增加内容
const addContent = () => {
  optionForm.content.push('')
}

// 点击提交
const submit = async () => {
  if (optionDoalogTitle.value === '新增') {
    addData(optionForm)
  } else if (optionDoalogTitle.value === '编辑') {
    const obj = JSON.parse(JSON.stringify(optionForm))
    obj.content = JSON.stringify(optionForm.content)
    submitUpdateData(obj)
  }
}

// 获得列表数据
watch(
  () => props.activeName,
  async () => {
    if (list.value.length > 0) return
    const res = await getList()
    list.value = res.value
  },
  {
    immediate: true
  }
)
</script>

<style scoped lang="less">
.el-pagination {
  display: flex;
  justify-content: center;
  margin-top: 10px;
}
.left_record {
  background-color: var(--bgColorTop);
  cursor: pointer;
  .text {
    color: var(--fontColorBase);
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }
  .add {
    width: 150px;
  }
  :deep .el-table__row {
    background-color: var(--bgColorTop) !important;
  }
  :deep .el-table__cell {
    color: var(--fontColorBase);
    background-color: var(--bgColorTop) !important;
  }
  :deep .el-table__cell {
    border-bottom-color: var(--bgColorBase) !important;
  }
  :deep .el-table__inner-wrapper::before {
    background-color: var(--bgColorTop);
  }
  :deep .el-dialog {
    background-color: var(--bgColorTop);
  }
  :deep .el-form-item__label {
    color: var(--fontColorBase);
  }
  :deep .el-textarea__inner {
    color: var(--fontColorBase);
    background-color: var(--bgColorTop);
  }
  :deep .el-dialog__title {
    color: var(--fontColorBase);
  }
  .poster {
    width: 100px;
  }
}
</style>
